package TankGame02;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/*
 *@Student 张策
 */
public class Mypanel extends JPanel implements KeyListener {
    //定义自己的坦克
    Hero hero=null;
    //定义敌人的坦克集合
    Vector<EnemyTank> enemyTanks = new Vector<>();
    int enemyTanksSize=3;
public  Mypanel(){
    //初始化
    hero=new Hero(100,100);
    hero.setSpeed(10);
    for (int i = 0; i < enemyTanksSize; i++) {
        EnemyTank enemyTank = new EnemyTank((i + 1) * 100, 0);
        enemyTank.setRect(1);
        enemyTanks.add(enemyTank);
    }
}
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1000,750);
        //绘制坦克
        drawTank(hero.getX(),hero.getY(),g,hero.getRect(),0);
        //敌人坦克
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank=enemyTanks.get(i);
            drawTank(enemyTank.getX(),enemyTank.getY(),g,enemyTank.getRect(),1);
        }
    }
    /*
     坐标，画笔，方向，类型
     */
    public void drawTank(int x,int y,Graphics g,int rect,int type){
        switch (type){
            case 0:
                g.setColor(Color.cyan);//w我方坦克
                break;
            case 1://敌人坦克
                g.setColor(Color.yellow);
                break;
        }
        switch (rect){
            case 0://方向向上
                g.fill3DRect(x,y,10,60,false);//左轮子
                g.fill3DRect(x+30,y,10,60,false);//右轮子
                g.fill3DRect(x+10,y+10,20,40,false);//躯干
                g.fillOval(x+10,y+20,20,20);//盖子
                g.drawLine(x+20,y,x+20,y+30);//炮筒
                break;
            case 1://方向向右
                g.fill3DRect(x,y,60,10,false);//左轮子
                g.fill3DRect(x,y+30,60,10,false);//右轮子
                g.fill3DRect(x+10,y+10,40,20,false);//躯干
                g.fillOval(x+20,y+10,20,20);//盖子
                g.drawLine(x+30,y+20,x+60,y+20);//炮筒
                break;
            case 2://方向向下
                g.fill3DRect(x,y,10,60,false);//左轮子
                g.fill3DRect(x+30,y,10,60,false);//右轮子
                g.fill3DRect(x+10,y+10,20,40,false);//躯干
                g.fillOval(x+10,y+20,20,20);//盖子
                g.drawLine(x+20,y+40,x+20,y+60);//炮筒
                break;
            case 3://方向向左
                g.fill3DRect(x,y,60,10,false);//左轮子
                g.fill3DRect(x,y+30,60,10,false);//右轮子
                g.fill3DRect(x+10,y+10,40,20,false);//躯干
                g.fillOval(x+20,y+10,20,20);//盖子
                g.drawLine(x+30,y+20,x,y+20);//炮筒
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }
    //按下某个键时触发
    @Override
    public void keyPressed(KeyEvent e) {
         if(e.getKeyCode()==KeyEvent.VK_W){
            hero.setRect(0);
            hero.moveup();
        } else if(e.getKeyCode()==KeyEvent.VK_D){
            hero.setRect(1);
            hero.moveright();
        } else if(e.getKeyCode()==KeyEvent.VK_S){
            hero.setRect(2);
            hero.movedown();
        } else if(e.getKeyCode()==KeyEvent.VK_A){
            hero.setRect(3);
            hero.movelest();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}

